package com.piesat.quake.business.device.model;

import com.baomidou.mybatisplus.annotation.TableField;
import com.piesat.quake.business.device.mqtt.LengthData;
import com.piesat.quake.business.device.mqtt.anno.CharField;
import com.piesat.quake.business.device.mqtt.anno.ConvertField;
import com.piesat.quake.business.device.mqtt.anno.LengthField;
import com.piesat.quake.business.device.mqtt.convert.TimestampConvert;

import java.io.Serializable;

/**
 * 报文通用字段
 *
 * @author Gersy
 * @date 2023/2/14
 * @since 1.0
 */
public class BaseModel implements Serializable, LengthData {
    protected static final long serialVersionUID = 1L;
    //类型标识	Char	2B	字符串内容为cs
    @CharField(2)
    @TableField(exist = false)
    protected String type;
    //数据包长度	Int	4B	包含类型标识在内的，整个数据包的长度
    @LengthField
    @TableField(exist = false)
    protected int length;
    //台站ID	Char	8B	格式为：xx.xxxxx（台网代码.台站代码），编码规则见地震行业的《地震数据通道编码》标准。
    @CharField(8)
    protected String id;
    //字序	Byte	1B	0 大端 1 小端
    @TableField(exist = false)
    protected byte endian = 1;
    //时间戳	Double	8B	格林威治时间1970年01月01日00时00分00秒（北京时间1970年01月01日08时00分00秒）起至数据包发送时的总秒数。
    @TableField(exist = false)
    @ConvertField(length = 8, convert = TimestampConvert.class)
    protected long timestamp;

    public BaseModel() {
    }

    public BaseModel(String type) {
        this.type = type;
        timestamp = System.currentTimeMillis();
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public int getLength() {
        return length;
    }

    public void setLength(int length) {
        this.length = length;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    @Override
    public byte getEndian() {
        return endian;
    }

    public void setEndian(byte endian) {
        this.endian = endian;
    }

    public long getTimestamp() {
        return timestamp;
    }

    public void setTimestamp(long timestamp) {
        this.timestamp = timestamp;
    }

    public void createTimestamp() {
        this.timestamp = System.currentTimeMillis();
    }

    @Override
    public String toString() {
        return "BaseModel{" +
                "type='" + type + '\'' +
                ", length=" + length +
                ", id='" + id + '\'' +
                ", endian=" + endian +
                ", timestamp=" + timestamp +
                '}';
    }
}
